﻿using System;
using System.Web;

namespace Shinkansen.Support
{
    public class PathResolver
    {
        /// <summary>
        /// Converts a relative path to absolute
        /// </summary>
        /// <param name="relativePath">The relative path.</param>
        /// <returns></returns>
        public string ToAbsolute (string relativePath)
        {
            if (string.IsNullOrEmpty (relativePath))
                throw new ArgumentNullException ("relativePath");

            string  path = relativePath;
            bool    isAbsolute = VirtualPathUtility.IsAbsolute (path);
            bool    isAppRelative = VirtualPathUtility.IsAppRelative (path);

            if (!isAbsolute && !isAppRelative)
                path = "~/" + path;

            return VirtualPathUtility.ToAbsolute (path);
        }

        /// <summary>
        /// Toes the physical path.
        /// </summary>
        /// <param name="absolutePath">The absolute path.</param>
        /// <returns></returns>
        public string ToPhysical (string absolutePath)
        {
            return HttpContext.Current.Server.MapPath (absolutePath);
        }
    }
}